// pages/index/compomemts/custom-list.js
import { getImages } from "../../../utils/images";
Component({
  lifetimes: {
    // 当组件实例刚刚被创建时
    created() {},
    // 组件实例进入页面节点树时
    attached() {},
    // 当组件在视图层布局完成后
    ready() {
      // console.log('this.properties.dataList',this.properties.dataList);
      // if(this.properties.type == '1') {
      //   this.setData({
      //     list: this.getnewList()
      //   })
      // } else {
      //   this.setData({
      //     list: this.properties.dataList
      //   })
      // }
    },
    // 当组件实例被从页面节点树移除时
    detached() {},
  },
  /**
   * 组件的属性列表
   */
  properties: {
    // 是否为我收藏的服务列表
    isMyCollect: {
      type: Boolean,
      value: false,
    },
    // 瀑布流列表展示的数据来源
    type: {
      type: String,
      value: "2", // 1默认示例
    },
    isLoading: {
      type: Boolean,
      value: false,
    },
    dataList: {
      type: Array,
      observer: function (newVal) {
        if (newVal && this.data.list !== newVal) {
          this.setData({
            list: newVal,
          });
        }
      },
    },
    isPageNum: {
      type: Boolean,
      value: false,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    crossAxisCount: 2,
    crossAxisGap: 8,
    mainAxisGap: 8,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toBuyService(e) {
      const { id, title, status } = e.currentTarget.dataset;
      if(status == 0) {
        return
      }
      wx.navigateTo({
        url: `/packageA/pages/service-detail/index?id=${id}&title=${title}`,
      });
    },
    binderror(event) {
      this.setData({
        error: "错误", //event.detail.errMsg
      });
    },
    bindscrolltolower(e) {
      console.log(e);
      this.setData({
        list: this.data.list.concat(this.getnewList()),
      });
    },

    getnewList() {
      const newList = new Array(20).fill(0);
      const imgUrlList = getImages();
      let count = 0;
      for (let i = 0; i < newList.length; i++) {
        newList[i] = {
          idx: i,
          title: `scroll-view`,
          sName: `默认只会渲染在屏节点，会根据直接子节点是否在屏来按需渲染`,
          sUnit: `/小时`,
          iPrice: 88,
          sFileUrl: imgUrlList[count++ % imgUrlList.length],
        };
      }
      return newList;
    },
  },
});
